﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using YJ_Code.Entities.Common;
using YJ_Code.Enums;
using YJ_Code.Tool;
using YJ_Core.Tool.Token;
using static YJ_App.Tool.Message.MessageDto;

namespace YJ_App.Tool.Message
{
    /// <summary>
    /// 站内消息服务
    /// </summary>
    [ApiDescriptionSettings(ApiGroupEnumsCode.Message, Name = "Message1")]
    public class MessageService : IDynamicApiController, ITransient, IMessageService
    {
        /// <summary>
        /// sql
        /// </summary>
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public MessageService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 创建站内消息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateMessage(CreateMessageInput input)
        {
            //去掉入参前后空格
            ToolHelper.TrimStrings(input);
            // 获取操作用户的信息
            var user = TokenService.GetClaimKey();

            var message = new MessageInstation()
            {
                Content = input.Content,
                CreateMan = user.UserName,
                UserId = input.UserId,
                IsRead = 0,
                MsgType = input.MsgType,
                Title = input.Title,
                CreateTime = DateTime.Now,
                IsDelete = false,
                MId = Guid.NewGuid().ToString("N"),
                TopicId = input.TopicId,
            };

            try
            {

                await _db.Insertable(message).ExecuteCommandAsync();

            }
            catch (Exception ex)
            {

                throw Oops.Bah("站内消息创建异常", ex);
            }
        }
    }
}
